<template>
  <div class="app-container">
    <xh-list-head
      @on-search="handleSearch"
      ref="TableHead"
      crmType="path"
      canSave
      :showdate="false"
      :options="selectOpt"
      :fielname="fielname"
      :listHander="listHander"
      placeholder="请输入点检项名称"
      @handleCreate="isCreate = true"
    />
    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="加载中"
      :height="tableHeight"
      use-virtual
      stripe
      border
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        align="center"
        width="55"
      />
      <el-table-column label="ID" align="center" fixed="left">
        <template slot-scope="scope">
          {{ scope.row.schid }}
        </template>
      </el-table-column>
      <el-table-column label="路径名称" align="center" fixed="left">
        <template slot-scope="scope">
          <div
            class="can-visit--underline"
            @click="handleEdit(scope.row, 'read')"
          >
            {{ scope.row.pathname }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.schedate }}
        </template>
      </el-table-column>
      <el-table-column label="执行周期" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.period }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行人" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.exename }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.sstatus | statusFilter">{{
            scope.row.sstatus | formatItemStatus
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row, 'update')"
            class="xm-btn--primary"
            type="primary"
            >编辑</el-button
          >
          <el-button
            size="mini"
            @click="deleteEvent(scope.$index, scope.row)"
            class="xm-btn--red"
            type="primary"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="p-contianer">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size.sync="pageSize"
        :total="total"
        :pager-count="5"
        class="p-bar"
        background
        layout="prev, pager, next, sizes, total, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新建/详情 -->
    <plan-create
      :visible="isCreate"
      @close="close"
      :detail="detailData"
      :action="action"
    />
  </div>
</template>

<script>
import TableMixin from "../mixins/Table";
import XhListHead from "@/components/XhListHead/index.vue";
import { getScheByCondPageAPI, deleteScheAPI } from "@/api/pathLine";
import planCreate from "./Create";
export default {
  name: "Plan",

  data() {
    return {
      list: null,
      listLoading: true,

      /**新建 */
      isCreate: false,
      detailData: {},
      action: {
        type: "save",
        data: {},
      },

      //状态
      selectOpt: [
        { value: null, label: "全部" },
        { value: "0", label: "禁用" },
        { value: "1", label: "启用" },
      ],

      //导出
      fielname: "执行计划",
      listHander: {
        pathname: "路径名称",
        period: "周期",
        schedate: "时间",
        exename: "执行人",
        sstatus: "状态",
      },
    };
  },
  mixins: [TableMixin],

  components: {
    XhListHead,
    planCreate,
  },
  mounted() {
    this.$nextTick(() => {
      this.getList();
    });
  },

  methods: {
    refresh() {
      this.action = {
        type: "save",
        data: {},
      };
      this.isCreate = false;
      this.getList(this.search);
    },
    /** tableData */
    getList(search) {
      this.listLoading = true;
      if (search && search.hasOwnProperty("dutystatus")) {
        search.sstatus = search.dutystatus;
        delete search.dutystatus;
      }
      getScheByCondPageAPI({
        pagenum: this.currentPage - 1,
        pagesize: this.pageSize,
        ...search,
      })
        .then((res) => {
          this.tableData = res.Data;
          this.total = res.Total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    //tree
    handleNodeClick(data) {
      if (data.Zoneid) {
        this.search.zoneid = data.Zoneid;
      } else {
        delete this.search.zoneid;
      }

      this.getList(this.search);
    },

    /**编辑 */
    handleEdit(item, type) {
      this.detailData = Object.assign({}, item);
      this.isCreate = true;
      this.$set(this.action, "type", type);
    },

    /**删除 */
    deleteEvent(index, item) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteScheAPI({ schid: item.schid }).then((res) => {
            this.getList(this.search);
            this.$message.success("删除成功");
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  width: 100%;
}
</style>